const mongoose = require('mongoose');
const UserModel = mongoose.model('User');
// const Message    = require('../proxy').Message;
const config     = require('../config');
const eventproxy = require('eventproxy');
const UserProxy  = require('../proxy').User;
const Response = require('../common/Response');


exports.gen_session = function(user, req, res) {
    let auth_token = user._id + '$$$$'; // 以后可能会存储更多信息，用 $$$$ 来分隔
    let opts = {
        path: '/',
        maxAge: 1000 * 60 * 60 * 24 * 30,
        signed: true,
        httpOnly: true
    };
    // if (req.cookies[config.auth_cookie_name]) {
    //     console.log(req.cookies);
    //     res.send("再次欢迎访问");
    // } else {
    //     res.cookie(config.auth_cookie_name, auth_token, opts);
    //     res.send("欢迎第一次访问");
    // }
    res.cookie(config.auth_cookie_name, auth_token, opts);//cookie 有效期30天
};

/**
 * 需要登录
 */
exports.userRequired = function (req, res, next) {
    if(!req.session || !req.session.user || !req.session.user._id) {
        let response = new Response(0, '', '当前用户未登录！');
        return res.send(response);
    }

    next();
};